package com.kingsalert.kingsalert.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.adapter.AddressListAdapter;
import com.kingsalert.kingsalert.util.AddsUtl_Tanz;
import com.kingsalert.kingsalert.util.http.HttpUtil;

import java.util.Arrays;
import java.util.List;


public class AddressSelectDialog_Tanzan extends BottomSheetDialog {

    private Context context;

    private TextView titleTv;
    private ListView listView;

    private TextView textView;

    private StringBuilder addressStrB = new StringBuilder();

    private String[] addressStrs = new String[2];

    private final OnAddressCheckFinishListener onAddressCheckFinishListener;

    private final String page;

    public AddressSelectDialog_Tanzan(@NonNull Context context, TextView textView, OnAddressCheckFinishListener onAddressCheckFinishListener,String page) {
        super(context, R.style.Theme_kingsalert_BottomDialogStyle);
        this.context = context;
        this.textView = textView;
        this.onAddressCheckFinishListener = onAddressCheckFinishListener;
        this.page = page;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_address_select);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        initView();
    }

    @SuppressLint({"SetTextI18n", "ResourceAsColor"})
    private void initView(){

        titleTv = findViewById(R.id.das_tv_title);
        listView = findViewById(R.id.das_lv_list);

        findViewById(R.id.das_ib_close).setOnClickListener(v -> dismiss());

        //选择市
        titleTv.setText("City");
        List<String> stateList = Arrays.asList(AddsUtl_Tanz.getInstance().getStates());
        listView.setAdapter(new AddressListAdapter(getContext(),stateList));

        listView.setOnItemClickListener((parent, view, position, id) -> {
            //保存市
            String city = stateList.get(position);
            addressStrB.append(city).append(" ");
            addressStrs[0] = city;

            HttpUtil.getInstance().saveBuriedPoint(page,"CITY_CLICK",city);

            //选择区
            titleTv.setText("Area");
            List<String> cityList =Arrays.asList(AddsUtl_Tanz.getInstance().getCities(city));
            listView.setAdapter(new AddressListAdapter(getContext(),cityList));

            listView.setOnItemClickListener((parent1, view1, position1, id1) -> {

                //保存区
                String area = cityList.get(position1);
                addressStrB.append(area);
                addressStrs[1] = area;

                HttpUtil.getInstance().saveBuriedPoint(page,"AREA_CLICK",area);

                //写入地址
                textView.setText(addressStrB.toString());

                onAddressCheckFinishListener.onFinish(addressStrs);

                dismiss();

            });

        });
    }

    public interface OnAddressCheckFinishListener{
        void onFinish(String[] addressStrs);
    }
}
